/////////////////////////////////////////////////////////////////////////////
// Copyright (c) 1999, COAS, Oregon State University  
// ALL RIGHTS RESERVED.   U.S. Government Sponsorship acknowledged. 
//
// Please read the full copyright notice in the file COPYRIGHT
// in this directory.
//
// Author: Nathan Potter (ndp@oce.orst.edu)
//
//                        College of Oceanic and Atmospheric Scieneces
//                        Oregon State University
//                        104 Ocean. Admin. Bldg.
//                        Corvallis, OR 97331-5503
//         
/////////////////////////////////////////////////////////////////////////////
//
// Based on source code and instructions from the work of:
//
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) 1998, California Institute of Technology.  
// ALL RIGHTS RESERVED.   U.S. Government Sponsorship acknowledged. 
//
// Please read the full copyright notice in the file COPYRIGHT
// in this directory.
//
// Author: Jake Hamby, NASA/Jet Propulsion Laboratory
//         Jake.Hamby@jpl.nasa.gov
/////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using NDODS.DAP;
using NDODS.DAP.Primitives;

namespace NDODS.DAP.BaseTypes
{
    /// <summary> Holds a DODS <code>Boolean</code> value.
    /// 
    /// </summary>
    /// <version>  $Revision: 1.5 $
    /// </version>
    /// <author>  npotter
    /// </author>
    /// <seealso cref="BaseType">
    /// </seealso>
    public class DBoolean : BaseType, IClientIO {
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1199'"
        /// <summary> Get the current value as a short (16bit int).</summary>
        /// <returns> the current value.
        /// </returns>
        /// <summary> Set the current value.</summary>
        public virtual bool Value {
            get { return val; }

            set { val = value; }
        }

        /// <summary> Returns the DODS type name of the class instance as a <code>String</code>.</summary>
        /// <returns> the DODS type name of the class instance as a <code>String</code>.
        /// </returns>
        public override String TypeName {
            get { return "Boolean"; }
        }

        /// <summary>Constructs a new <code>DBoolean</code>. </summary>
        public DBoolean() : base() {}

        /// <summary> Constructs a new <code>DBoolean</code> with name <code>n</code>.</summary>
        /// <param name="n">the name of the variable.
        /// </param>
        public DBoolean(String n) : base(n) {}

        /// <summary>The value of this <code>DBoolean</code>. </summary>
        private bool val;

        /// <summary> Constructs a new <code>BooelanPrimitiveVector</code>.</summary>
        /// <returns> a new <code>BooelanPrimitiveVector</code>.
        /// </returns>
        public override PrimitiveVector NewPrimitiveVector() {
            return new BooleanPrimitiveVector(this);
        }


        /// <summary> Prints the value of the variable, with its declaration.  This
        /// function is primarily intended for debugging DODS applications and
        /// text-based clients such as geturl.
        /// 
        /// </summary>
        /// <param name="os">the <code>PrintWriter</code> on which to print the value.
        /// </param>
        /// <param name="space">this value is passed to the <code>printDecl</code> method,
        /// and controls the leading spaces of the output.
        /// </param>
        /// <param name="print_decl_p">a bool value controlling whether the
        /// variable declaration is printed as well as the value.
        /// </param>
        /// <seealso cref="BaseType.PrintValue(StreamWriter,string,bool)">
        /// </seealso>
        public override void PrintValue(StreamWriter os, String space, bool print_decl_p) {
            if (print_decl_p) {
                PrintDecl(os, space, false);
                //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
                os.WriteLine(" = " + val + ";");
            }
            else
                os.Write(val);
        }

        /// <summary> Reads data from a <code>DataInputStream</code>. This method is only used
        /// on the client side of the DODS client/server connection.
        /// 
        /// </summary>
        /// <param name="source">a <code>DataInputStream</code> to read from.
        /// </param>
        /// <param name="sv">the <code>ServerVersion</code> returned by the server.
        /// </param>
        /// <param name="statusUI">the <code>IStatusUI</code> object to use for GUI updates
        /// and user cancellation notification (may be null).
        /// </param>
        /// <exception cref="IOException">thrown on any other InputStream exception.
        /// </exception>
        /// <seealso cref="IClientIO.Deserialize">
        /// </seealso>
        //UPGRADE_TODO: Class 'java.io.DataInputStream' was converted to 'System.IO.BinaryReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioDataInputStream'"
        //UPGRADE_NOTE: Synchronized keyword was removed from method 'deserialize'. Lock expression was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1027'"
        public virtual void Deserialize(BinaryReader source, ServerVersion sv, IStatusUI statusUI) {
            lock (this) {
                val = source.ReadBoolean();
                if (statusUI != null)
                    statusUI.IncrementByteCount(1);
            }
        }

        /// <summary> Writes data to a <code>DataOutputStream</code>. This method is used
        /// primarily by GUI clients which need to download DODS data, manipulate 
        /// it, and then re-save it as a binary file.
        /// 
        /// </summary>
        /// <param name="sink">a <code>DataOutputStream</code> to write to.
        /// </param>
        /// <exception cref="IOException">thrown on any <code>OutputStream</code>
        /// exception. 
        /// </exception>
        //UPGRADE_TODO: Class 'java.io.DataOutputStream' was converted to 'System.IO.BinaryWriter' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioDataOutputStream'"
        public virtual void Externalize(BinaryWriter sink) {
            sink.Write(val);
        }

        //UPGRADE_TODO: The following method was automatically generated and it must be implemented in order to preserve the class logic. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1232'"
        public override Object Clone() {
            return null;
        }


        public override string FancyTypeName {
            get { return "Boolean"; }
        }
    }
}